package com.example.prison.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.prison.entity.AreaManagement;
import com.example.prison.entity.DefenceArea;
import com.example.prison.mapper.AreaManagementMapper;
import com.example.prison.service.DefenceAreaService;
import com.example.prison.mapper.DefenceAreaMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
* @author jiang
* @description 针对表【defence_area】的数据库操作Service实现
* @createDate 2024-06-20 14:52:58
*/
@Service
public class DefenceAreaServiceImpl extends ServiceImpl<DefenceAreaMapper, DefenceArea>
    implements DefenceAreaService{

    @Autowired
    private DefenceAreaMapper defenceAreaMapper;

    @Autowired
    private AreaManagementMapper areaManagementMapper;

    @Override
    public PageInfo<DefenceArea> get(int pageNumber, int pageSize, String name, String type) {
        PageHelper.startPage(pageNumber, pageSize);
        return new PageInfo<>(defenceAreaMapper.get(name, type));
    }

    @Override
    public List<AreaManagement> getUnmarkArea(){
        return areaManagementMapper.getUnmarkArea();
    }

    @Override
    public int add(DefenceArea data) {
        defenceAreaMapper.add(data);
        if(ObjectUtils.isNotEmpty(data.getName()) && ObjectUtils.isNotEmpty(areaManagementMapper.getByDefenseSection(data.getName()))) {
            areaManagementMapper.setMark(data.getName(), data.getId());
        }
        return 1;
    }

    @Override
    public void delete(String[] ids) {
        for (String id : ids) {
            if(ObjectUtils.isNotEmpty(defenceAreaMapper.getById(String.valueOf(id)) )) {
                DefenceArea data = defenceAreaMapper.getById(String.valueOf(id));
                if (ObjectUtils.isNotEmpty(data.getName()) && ObjectUtils.isNotEmpty(areaManagementMapper.getByDefenseSection(data.getName()))) {
                    areaManagementMapper.setUnMark(data.getName());
                }
            }
        }
        defenceAreaMapper.deleting(ids);
    }

    @Override
    public void updating(DefenceArea data, String id) {
        defenceAreaMapper.updating(data, id);
    }


    @Override
    public DefenceArea getById(String id) {
        return defenceAreaMapper.getById(id);
    }

    @Override
    public DefenceArea getByName(String name){
        return defenceAreaMapper.getByName(name);
    }

    @Override
    public void deleteAll() {
        defenceAreaMapper.deleteAll();
    }

}




